using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework.Interfaces;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.UIElements;
using Image = UnityEngine.UI.Image;

public class InventoryItem : MonoBehaviour
{
    public String uuIdName;
    public ItemData itemData;
    private Image image;

    public bool needRaycast = false;
    public bool isHide = true;
    public int hideLoadNum ;

    public Sprite hideSprite;

    public ItemGrid lastItemGrid;
    public Vector2Int lastItemPosition;

    RectTransform RectTransform;

    public int HEIGHT
    {
        get {
            if (rotated == false) {
                return itemData.sizeHeight;
            }
            return itemData.sizeWidth;
        }
    }

    public int WIDTH
    {
        get
        {
            if (rotated == false)
            {
                return itemData.sizeWidth;
            }
            return itemData.sizeHeight;
        }
    }

    public int onGridPositionX;
    public int onGridPositionY;
    public bool rotated = false;
    public bool lastRotated = false;

    private void Start()
    {
        image = GetComponent<Image>();
        RectTransform = GetComponent<RectTransform>();
    }

    private void Update()
    {
        if (needRaycast && !isHide)
        {
            image.raycastTarget = true;
        }
        else {
            image.raycastTarget = false;
        }

        
    }

    public void set(ItemData itemData,ItemGrid itemGrid)
    {
        this.itemData = itemData;
        GetComponent<Image>().sprite = itemData.itemIcon;
        this.hideLoadNum = itemData.hideCount;

        Vector2 size = new Vector2();
        size.x = WIDTH * itemGrid.tileSizeWidth;
        size.y = HEIGHT * itemGrid.tileSizeHeight;
        GetComponent<RectTransform>().sizeDelta = size;
    }

    public void set(ItemData itemData,int tileSizeWidth,int tileSizeHeight)
    {
        this.itemData = itemData;
        GetComponent<Image>().sprite = itemData.itemIcon;
        this.hideLoadNum = itemData.hideCount;

        Vector2 size = new Vector2();
        size.x = WIDTH * tileSizeWidth;
        size.y = HEIGHT * tileSizeHeight;
        GetComponent<RectTransform>().sizeDelta = size;
    }

    public void setOther(ItemData itemData, ItemGrid itemGrid)
    {
        this.itemData = itemData;
        GetComponent<Image>().sprite = itemData.itemIcon;
        this.hideLoadNum = itemData.hideCount;

        Vector2 size = new Vector2();
        size.x =  itemGrid.tileSizeWidth;
        size.y = itemGrid.tileSizeHeight;
        GetComponent<RectTransform>().sizeDelta = size;
    }

    public void setIsHide(bool hideStatus) {
        isHide = hideStatus;
        if (hideStatus)
        {
            GetComponent<Image>().sprite = hideSprite;
        }
        else { 
        GetComponent<Image>().sprite = itemData.itemIcon;
        }
    }

    public void Rotate(bool needRotate)
    {
        rotated = needRotate;
        RectTransform rectTransform = GetComponent<RectTransform>();
        rectTransform.rotation = Quaternion.Euler(0, 0, rotated == true ? 90f : 0);
    }

    public void setLastItemPosition(int x , int y) {
        Vector2Int vector2Int = new Vector2Int(x,y);
        lastItemPosition = vector2Int;
    }

    public void setLastItemGrid(ItemGrid itemGrid) {
        lastItemGrid = itemGrid;
    }

    public void setParent(ItemGrid targetGrid) {
        if (targetGrid == null) { return; }
        RectTransform.SetParent(targetGrid.GetComponent<RectTransform>());
    }

    //public void PointerEnter(BaseEventData baseEventData) {

    //    InventoryItem inventoryItem = this;
    //    Debug.Log(this.itemData.name + "enter");

    //    string name = inventoryItem.itemData.productName;
    //    int price = inventoryItem.itemData.price;
    //    string describe = inventoryItem.itemData.describe;

    //    GameObject allText = GameObject.FindGameObjectWithTag("gridText");
    //    Vector2 mousePostion = Input.mousePosition;
    //    mousePostion = mousePostion - new Vector2(-60, 50);
    //    allText.GetComponent<RectTransform>().position = mousePostion;

    //    Text[] text = allText.GetComponentsInChildren<Text>();
    //    foreach (Text content in text)
    //    {
    //        if (content.name == "productName")
    //        {
    //            content.text = name;
    //        }
    //        if (content.name == "productDetail")
    //        {
    //            content.text = describe;
    //        }
    //        if (content.name == "productPrice")
    //        {
    //            content.text = price + "";
    //        }
    //    }
    //}

    //public void PointerExit(BaseEventData baseEventData) {
    //    Debug.Log(this.itemData.name + "out");

    //    GameObject allText = GameObject.FindGameObjectWithTag("gridText");
    //    Vector2 mousePostion = Input.mousePosition;
    //    mousePostion = mousePostion - new Vector2(-1000, 1000);
    //    allText.GetComponent<RectTransform>().position = mousePostion;

    //    Text[] text = allText.GetComponentsInChildren<Text>();

    //    foreach (Text content in text)
    //    {
    //        if (content.name == "productName")
    //        {
    //            content.text = null;
    //        }
    //        if (content.name == "productDetail")
    //        {
    //            content.text = null;
    //        }
    //        if (content.name == "productPrice")
    //        {
    //            content.text = null;
    //        }
    //    }
    //}
}
